﻿using LCL.CodeHelper;
using LCL.Config;
using LCL.IBuilder;
using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;

namespace LCL.Tools.WinFrm
{
    public partial class FrmProjectCodeBuilder : BaseFrm
    {
        private delegate void SetBtnDisableCallback();
        private delegate void SetBtnEnableCallback();

        private Thread mythread;
        DbConfigInfo dbConfig = null;      
        public FrmProjectCodeBuilder()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            dbConfig = DbConfig.GetConfig();

            this.label4.Text = DbConfig.GetConfig().DbHost;
            this.txtNamespace.Text = AppConfig.GetInstance().NameSpace;
            this.txtTableFormat.Text = AppConfig.GetInstance().TableFormat;

            UIFactory.UIFact().BindDataBaseList(this.cmbDBlist);
            UIFactory.UIFact().BindBuilderEngineList(this.cmbBuilderEngine);
        }
        private void cmbDBlist_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (cmbDBlist.SelectedItem == null) return;
                string dbName = cmbDBlist.SelectedItem.ToString();
                string strfir = dbName.Substring(0, 1).ToUpper();
                string newTabName = strfir + dbName.Substring(1);
                this.txtNamespace.Text = newTabName;
            }
            catch { }
        }
        private void but_expCode_Click(object sender, EventArgs e)
        {
            object obj = this.cmbDBlist.SelectedItem;
            if (obj == null)
            {
                MessageBox.Show("请选择数据库", "提示");
                return;
            }
            obj = this.cmbBuilderEngine.SelectedItem;
            if (obj == null)
            {
                MessageBox.Show("请选择分层模式", "提示");
                return;
            }

            this.mythread = new Thread(new ParameterizedThreadStart(this.ThreadWork));
            this.mythread.Start(obj);
        }
        public void SetBtnDisable()
        {
            if (this.but_expCode.InvokeRequired)
            {
                SetBtnDisableCallback method = new SetBtnDisableCallback(this.SetBtnDisable);
                base.Invoke(method, null);
                return;
            }
            this.but_expCode.Enabled = false;
        }
        public void SetBtnEnable()
        {
            if (this.but_expCode.InvokeRequired)
            {
                SetBtnEnableCallback method = new SetBtnEnableCallback(this.SetBtnEnable);
                base.Invoke(method, null);
                return;
            }
            this.but_expCode.Enabled = true;
        }
        private void ThreadWork(object obj)
        {
            this.SetBtnDisable();
            BuilderDescriptor _builderDescriptor = ((LParam)obj).Value as BuilderDescriptor;
            if (_builderDescriptor != null)
            {
                IProjectBuilder objType = _builderDescriptor.ProjectBuilder;
                objType.ConnectionString = dbConfig.ConnectionString;
                objType.DbType = dbConfig.DbType;
                objType.DbHost = dbConfig.DbHost;
                objType.DbUser = dbConfig.DbUser;
                objType.DbPwd = dbConfig.DbPwd;

                objType.DbName = this.cmbDBlist.SelectedItem.ToString();
                objType.TableFormat = this.txtTableFormat.Text;
                objType.NameSpace = this.txtNamespace.Text;

                objType.Generate();

                this.SetBtnEnable();
                MessageBox.Show(this, objType.Name + "项目工程生成成功！", "完成", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                base.Close();
            }
            else
            {
                this.SetBtnEnable();
                MessageBox.Show("请选择分层模式", "提示");
            }
        }
    }
}
